﻿$(document).ready(function () {
    $("#btnAddNew").click(function () {
        if ($(this).val() == "Add new") {
            $("#addnewbox").show();
            $(this).val("Hide");
        }
        else {
            $("#addnewbox").hide();
            $(this).val("Add new");
        }
    });
   
    //edit all
    if (parseInt(nPermissions) >= 3) {
        $("#btnupdate").click(function () {
            if ($("#btnupdate").val() == "Edit") {
                $('table[editcellcontent="true"]').InitialEditTableCell({ emptytext: "" });
                $("#btnupdate").val("Done");
                $(".changed").addClass("status");
            }
            else {
                setTimeout(function () {
                    $("#btnupdate").val("Edit");
                    $(".changed").removeClass("status");
                    SaveChangeItems();
                }, 50);
            }
        });
    } else {
        $("#btnupdate").remove();
    }

});
function EditInline(row, sender) {
    $(row).DoPendingChange();
    var ischange = $(row).attr("changed");
    if ((typeof ischange == "undefined" || ischange == "") && $(sender).text() != "Done") {
        $(row).InitialEditTableCell({ emptytext: '', inline: true });
        $(row).addClass("editing");
        $(sender).text("Done");
    } else if (ischange == "true") {
        SaveInline(row, sender);
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
        $(row).removeClass("editing");
    }
}

function SaveInline(row, sender) {
    if (typeof $(row).attr("changed") != "undefined" && $(row).attr("changed") == "true") {
        var ID = $.trim($(row).attr("ColLoaderID"));
        var Carr = $.trim($(row).children('[code="Carr"]').text());
        var Port = $.trim($(row).children('[code="Port"]').text());
        var Pic_Sales = $.trim($(row).children('[code="Pic_Sales"]').text());
        var Tel = $.trim($(row).children('[code="Tel"]').text());
        var Cell = $.trim($(row).children('[code="Cell"]').text());
        var Charge = $.trim($(row).children('[code="Charge"]').text());
        var Route = $.trim($(row).children('[code="Route"]').text());
        var Skype = $.trim($(row).children('[code="Skype"]').text());
        var Email = $.trim($(row).children('[code="Email"]').text());

        $.ajax({
            type: 'POST',
            url: "Handlers/EditInline.ashx",
            data: {
                act: "colloader",
                Carr: Carr,
                ID: ID,
                Port: Port,
                Pic_Sales: Pic_Sales,
                Tel: Tel,
                Cell: Cell,
                Charge: Charge,
                Route: Route,
                Skype: Skype,
                Email: Email
            },
            success: function (data) {
                if (data == null) {
                    alert("Cập nhật không thành công!");
                } else if (data.Message == "sucessful") {
                    $(row).children('td[code="LastUpdate"]').text(data.Date);
                } else {
                    alert(data.Message);
                }

                $(row).removeAttr("changed");
                $(row).DisableEditTableCell();
                $(sender).text("Edit");
            },
            dataType: "json"
        });
    } else {
        $(sender).text("Edit");
        $(row).DisableEditTableCell();
    }
}

function SaveChangeItems() {
    var rowchanges = $('tr[changed="true"]');
    $('table[editcellcontent="true"]').DisableEditTableCell();
    if ($(rowchanges).length == 0) {
        return;
    }
    var temp = "";
    var items = new Array();
    var changecells = new Array();
    var _cell;
    var doSave = true;
    $.each(rowchanges, function (i, row) {
        var ID = $.trim($(row).attr("colloaderid"));
        var Carr = $.trim($(row).children('[code="Carr"]').text());
        var Port = $.trim($(row).children('[code="Port"]').text());
        var Pic_Sales = $.trim($(row).children('[code="Pic_Sales"]').text());
        var Tel = $.trim($(row).children('[code="Tel"]').text());
        var Cell = $.trim($(row).children('[code="Cell"]').text());
        var Charge = $.trim($(row).children('[code="Charge"]').text());        
        var Skype = $.trim($(row).children('[code="Skype"]').text());
        var Email = $.trim($(row).children('[code="Email"]').text());

        items.push("{'ID':'" + ID
                    + "','Carr':'" + Encoder.htmlEncode(Carr)
                    + "','Port':'" + Encoder.htmlEncode(Port)
                    + "','Pic_Sales':'" + Encoder.htmlEncode(Pic_Sales)
                    + "','Tel':'" + Encoder.htmlEncode(Tel)
                    + "','Cell':'" + Encoder.htmlEncode(Cell)
                    + "','Charge':'" + Encoder.htmlEncode(Charge)
                    + "','Skype':'" + Encoder.htmlEncode(Skype)
                    + "','Email':'" + Encoder.htmlEncode(Email)
                    + "','Description':'" + ""
                    + "','Sort':'" + 0
                    + "'}");
    });

    $.ajax({
        type: 'POST',
        url: "Handlers/EditInline.ashx",
        data: { items: "[" + items.toString() + "]", act: "colloader", version: "2" },
        success: function (data) {
            if (data != null && data == "NAME_BLANK") {
                alert("Tên khách hàng không được để trống!");
            } else {
                $('tr[changed="true"]').removeAttr("changed");
                $(".changed").removeClass("status");
            }
        },
        dataType: "json"
    });
}